package ces.lawsuit.config;


import org.beetl.core.GroupTemplate;
import org.beetl.ext.jfinal.BeetlRenderFactory;

import com.jfinal.config.Constants;
import com.jfinal.config.Handlers;
import com.jfinal.config.Interceptors;
import com.jfinal.config.JFinalConfig;
import com.jfinal.config.Plugins;
import com.jfinal.config.Routes;
import com.jfinal.ext.handler.ContextPathHandler;
import com.jfinal.kit.PropKit;
import com.jfinal.plugin.activerecord.CaseInsensitiveContainerFactory;
import com.jfinal.plugin.activerecord.dialect.MysqlDialect;
import com.jfinal.plugin.activerecord.dialect.OracleDialect;
import com.jfinal.plugin.activerecord.dialect.SqlServerDialect;
import com.jfinal.plugin.activerecord.tx.TxByMethods;
import com.jfinal.plugin.c3p0.C3p0Plugin;

import ces.lawsuit.controller.IndexController;
import ces.lawsuit.interceptor.ExceptionInterceptor;
import ces.lawsuit.interceptor.LoginInterceptor;
import ces.lawsuit.plugins.AutoTableBindPlugin;
import ces.lawsuit.util.App;
import ces.lawsuit.util.AutoControllerRegist;
import ces.lawsuit.util.TableNameStyle;


/***
 *@author 于兴兵
 *@Describe jfinal 基础配置
 */
public class LawsuitConfig extends JFinalConfig {

	@Override
	public void configConstant(Constants me) {
		PropKit.use("db.properties");
		me.setDevMode(PropKit.getBoolean("devMode", false));
		me.setEncoding("utf-8");
	//	me.setBaseViewPath("WEB-INF/view");
		me.setMainRenderFactory(new BeetlRenderFactory());
		// 获取GroupTemplate ,可以设置共享变量等操作
		GroupTemplate groupTemplate = BeetlRenderFactory.groupTemplate ;
		
	}

	@Override
	public void configRoute(Routes me) {
		me.add("/",IndexController.class);
		AutoControllerRegist.regist(me);
	}

	@Override
	public void configPlugin(Plugins me) {
		// TODO Auto-generated method stub
		C3p0Plugin c3p0Plugin = new C3p0Plugin(PropKit.get(App.DB_CONNECTION_URL), PropKit.get(App.DB_CONNECTION_USERNAME), PropKit.get(App.DB_CONNECTION_PASSWORD).trim(),PropKit.get(App.DB_CONNECTION_DRIVERCLASS));
		me.add(c3p0Plugin);
		
		// 配置ActiveRecord插件
		//ActiveRecordPlugin arp = new ActiveRecordPlugin(c3p0Plugin);
		
		AutoTableBindPlugin autoTableBindPlugin = new AutoTableBindPlugin(
				c3p0Plugin, TableNameStyle.UP);
		String dbType=PropKit.get(App.DB_TYPE_KEY);
		if(App.DB_TYPE_MYSQL.equalsIgnoreCase(dbType)){
			autoTableBindPlugin.setDialect(new MysqlDialect());
		}else if(App.DB_TYPE_ORACLE.equalsIgnoreCase(dbType)){
			autoTableBindPlugin.setDialect(new OracleDialect());
		}else if(App.DB_TYPE_SQLSERVER.equalsIgnoreCase(dbType)){
			autoTableBindPlugin.setDialect(new SqlServerDialect());
		}
		//autoTableBindPlugin.setDialect(new OracleDialect());
		autoTableBindPlugin.setShowSql(PropKit.get(App.DB_SHOW_SQL).equalsIgnoreCase("true"));
		autoTableBindPlugin.setContainerFactory(new CaseInsensitiveContainerFactory());
//		autoTableBindPlugin.setDialect(new OracleDialect());
//		autoTableBindPlugin.setContainerFactory(new CaseInsensitiveContainerFactory());
		me.add(autoTableBindPlugin);
		
		
		//arp.addMapping("blog", BaseModel.class);	// 映射blog 表到 Blog模型
	}

	@Override
	public void configInterceptor(Interceptors me) {
		// TODO Auto-generated method stub
		me.addGlobalActionInterceptor(new ExceptionInterceptor());
		me.add(new TxByMethods("save", "update","saveOrUpdate","delete"));
		//me.addGlobalActionInterceptor(new LoginInterceptor());
	}

	@Override
	public void configHandler(Handlers me) {
		// TODO Auto-generated method stub
		me.add(new ContextPathHandler());
	}
	

}
